<template>
	<div id="course_detail">
		<div class="content">
			<div class="banner">
				
				<div class="enroll">
					<div class="title">立即报名暑期课程</div>
					
					<div class="row row_1">
						<div class="icon"></div>
						<span class="prefix">86+</span>
						<input class="input" v-model="param.phoneNumber" id="phoneNumber" type="text"
							   autocomplete="off" spellcheck="false" placeholder="输入手机号码" maxLength="20"/>
					</div>
					<div class="row row_2">
						<div class="icon"></div>
						<input class="input" v-model="param.captcha" id="captcha" type="text"
							   autocomplete="off" spellcheck="false" placeholder="输入短信验证码" maxLength="20"/>
						<div class="code" :class="codeTime>0?'code_wait':''" @click="getCode()">{{codeMsg}}</div>
					</div>
					
					<div class="sure_btn" @click="register()">报 名</div>
				
				</div>
				<div class="banner_panel">
					<div class="banner1_content">
						<div class="banner1_btn"></div>
					</div>
				</div>
			</div>
			
			<div class="stage">
				<div class="stage_title">哈佛案例教学法</div>
				<div class="stage_content">
					根据我国教育部《3-6岁儿童学习与发展指南》，融合美国共同核心州立标准CCSS，采用哈佛案例教学法，通过思维培养，
					让孩子学会发现问题，处理问题和解决问题的能力。
				</div>
				<div class="stage_img"></div>
			</div>
			
			<div class="center">
				
			</div>
			
			
			<div class="teacher">
				<div class="teacher_title">与清华北大名师直接对话</div>
				<div class="teacher_content">
					<div class="teacher_panel">
						<div class="teacher_img"></div>
						<div class="teacher_name">张洁</div>
						<div class="teacher_text">
							金色摇篮全程实验学校校长<br>
							23年一线教育教学经验，荣获“北京市金牌教师”等称号
						</div>
					</div>
					<div class="teacher_panel">
						<div class="teacher_img"></div>
						<div class="teacher_name">李桓</div>
						<div class="teacher_text">
							国内幼小衔接高级专家<br/>
							20年儿童教育从教经验
						</div>
					</div>
					<div class="teacher_panel">
						<div class="teacher_img"></div>
						<div class="teacher_name">徐健峰</div>
						<div class="teacher_text">
							中国人民大学高材生<br/>
							儿童教育产品负责人
						</div>
					</div>
				</div>
			</div>
			
			
			<div class="bottom_seat"></div>
			<div class="bottom" :class="isFixed? 'bottom_fixed':''">
				<div class="bottom_content">
					<div class="bottom_logo"></div>
				</div>
			</div>
		</div>
		
		<div class="dialog animated" :class="open? 'fadeIn':'fadeOut'"
			 v-show="show" :style="{visibility:(visible?'visible':'hidden')}">
			 
			<div class="panel animated" :class="open? 'zoomIn':''">
				<div class="panel_close" @click="closeDialog()"></div>
				<div class="panel_success"></div>
				<div class="panel_title">
					报名信息提交成功
				</div>
				<div class="panel_text">
					您的课程顾问会在24小时内联系您，请耐心等待。
					<br/>
					您也可以拨打我们的服务热线
					<br/>
					400-12345654
				</div>
			</div>
		</div>
		
		<advert></advert>
	</div>
</template>

<script>
import advert from '../common/advert'

var vm;
var $homeContent,$courseDetail;

export default {
	components : {
		advert
	},
	data : function() {
		return {
			isFixed : false,
			param : {
				phoneNumber : "",
				captcha : ""
			},
			codeMsg : "获取验证码",
			codeTime : 0,
			show : false,
			open : false,
			visible : false
		}
	},
	methods : {
		setFixed : function() { //设置底部栏定位样式
			//内容整体高度
			var h1 = $courseDetail.outerHeight();
			//容器高度+滚动高度
			var h2 = $homeContent.height() + $homeContent.scrollTop();
			//大于,说明内容还有部分没展示,即底部栏还未出现,固定样式
			vm.isFixed = (h1 > h2)
		},
		getCode : function() {
			//等待验证码过程,不允许再点
			if(vm.codeTime>0) {
				return;
			}
			
			var rules = [{
				judge : tool.judge("phone",vm.param.phoneNumber),
				focus : $("#phoneNumber"),
				tips : "请输入正确的手机号码"
			}];
			if(!tool.validate(rules)) {
				return;
			}
			
			//请求发送验证码接口
			api.captcha({phoneNumber : vm.param.phoneNumber}).then(function(data){
				
				if(data.resultCode == 4025) {
					tool.message({type:"warning",message:"短信发送失败，请检查手机号码是否正确"});
				}
				if(data.resultCode == 4033) {
					tool.message({type:"warning",message:"获取验证码操作频繁，请稍候再试"});
				}
				if(data.resultCode == 2000) {
					tool.message({type:"success",message:"已发送验证码"});
					//重设等待时间,执行等待循环
					vm.codeTime = 61;
					vm.waitCode();
				}
			})
			
		},
		waitCode : function() {
			vm.codeTime--;
			
			//还未减到0,更新提示时间
			if(vm.codeTime>0) {
				vm.codeMsg = vm.codeTime+" s"
				setTimeout(vm.waitCode,1000);
				return;
			}
			//已减到0,复原状态
			vm.codeMsg = "获取验证码";
		},
		register : function() {
			var rules = [{
				judge : tool.judge("phone",vm.param.phoneNumber),
				focus : $("#phoneNumber"),
				tips : tips.phone
			},{
				judge : vm.param.captcha,
				focus : $("#captcha"),
				tips : tips.captcha
			}];
			if(!tool.validate(rules)) {
				return;
			}
			
			api.register(vm.param).then(function(data) {
				if(data.resultCode == 4026) {
					tool.message({type:"warning",message:"验证码错误"});
				}
				
				if(data.resultCode == 2000 || data.resultCode == 4028) {
					vm.param.phoneNumber = "";
					vm.param.captcha = "";
					
					vm.codeTime = 0;
					vm.codeMsg = "获取验证码";
					
					vm.openDialog();
					//10秒自动关闭
					setTimeout(function(){
						vm.closeDialog();
					},10000)
				}
			})
		},
		openDialog : function() {
			vm.show = true;
			setTimeout(function() { 
				vm.open = true; 
				vm.visible = true;
			},1);
		},
		closeDialog : function() {
			vm.open = false;
			setTimeout(function() {
				vm.show = false;
				vm.visible = false;
			},500);
		}
	},
	mounted : function() {
		$homeContent = $(".home_content");
		$courseDetail = $("#course_detail");
		
		//页面初始设置
		vm.setFixed();
		//监听滚动事件和窗口大小改变事件
		$homeContent.scroll(vm.setFixed);
		$(window).resize(function() {
			vm.setFixed();
		});

		
		//承诺栏,鼠标上移添加动画
		$(".promise_detail").hover(function() {
			var $img = $(this).find(".promise_img");
			$img.addClass("swing");
			//动画结束后移除
			setTimeout(function(){
				$img.removeClass("swing");
			},1000)
		},function(){})
		
	},
	created : function() {
		vm = this;
	}
}

</script>

<style scoped lang="scss" src="../../style/home_page/course_detail.css"></style>